package com.jcgyl.export.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jcgyl.export.entity.TbInventoryHead;
import com.jcgyl.export.entity.Tbreceipt;
import com.jcgyl.export.model.inventory.InventoryModel;
import com.jcgyl.export.model.payinfo.ReceiptModel;
import com.jcgyl.export.service.InventoryService;
import com.jcgyl.export.util.entity.JsonResult;
import com.jcgyl.export.util.entity.JsonUtil;
import com.jcgyl.export.util.entity.PageResult;

@Controller
@RequestMapping("/inventory/")
public class InventoryController {
	@Autowired
	private InventoryService InventoryService;
	@GetMapping("query")
	public String Query() {
		return "inventory/query";
	}
	/**
	 * 根据条件查询数据
	 * @param inventoryModel 传入自定义的清单接受数据
	 * @return
	 * JL
	 */
	@RequestMapping("doFindInventory")
	@ResponseBody
	public PageResult doFindInventory(@RequestBody InventoryModel inventoryModel){
		//获取前端传入的数据
		Integer pageCount = inventoryModel.getPageIndex();
		Integer pageSize = inventoryModel.getPageSize();
		String orderNo = inventoryModel.getFilter().getOrderNo();
		String  batchNumber = inventoryModel.getFilter().getBatchNumber();
		String transferNumber = inventoryModel.getFilter().getTransferNumber();
		String Starttime = inventoryModel.getFilter().getStart();
		String Endtime = inventoryModel.getFilter().getEnd();
		List<TbInventoryHead>list = InventoryService.findInventory(orderNo, batchNumber, transferNumber, Starttime,Endtime, pageSize, pageCount);
		int row = InventoryService.getRowCount(orderNo, batchNumber, transferNumber, Starttime,Endtime);
		PageResult jsonResult = new PageResult();
		jsonResult.setResult(list);
		jsonResult.setTotalRows(row);
		return jsonResult;
	}
	/**
	 * 清单回执查询
	 * @param receiptModel 清单接收类型
	 * @return
	 * JL
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("doFindReceipt")
	@ResponseBody
	public JsonResult doFindReceipt(@RequestBody ReceiptModel receiptModel) {
		String expressnumber = receiptModel.getGuid();
		System.out.println(expressnumber);
		String receipttype = "BGM001_BACK";
		List<Tbreceipt> list = InventoryService.findReceipt(receipttype, expressnumber);
		if(list.size() == 0){
			return JsonUtil.error(1, "暂无清单回执信息");
		}else{
			return JsonUtil.success(list);
		}
	}
	/**
	 * 修改清单信息
	 * @param tbInventoryHead
	 * @return
	 * JL
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("doUpdateInventory")
	@ResponseBody
	public JsonResult doUpdateInventory(@RequestBody TbInventoryHead tbInventoryHead){
		int row = InventoryService.updateByPrimaryKeySelective(tbInventoryHead);
		if(row !=1){
			return JsonUtil.error(1, "修改信息失败");
		}else{
			return JsonUtil.success(row);
		}
	}
}
